package com.aaa.service.Impl;

import com.aaa.common.domain.*;
import com.aaa.common.result.AssetQuery;
import com.aaa.common.result.Result;
import com.aaa.mapper.*;
import com.aaa.service.AssetService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @className AssetServiceImpl
 * @Description TODO
 * @Author wyy
 * @Data 2021/1/20 20:32
 * @Version 1.0
 */
@Service
public class AssetServiceImpl implements AssetService {
    //公有 私有
    @Autowired
    private  AssetBasicInfoMapper assetBasicInfoMapper;
    //有证资产
    @Autowired
    private EcoAssetInfoMapper ecoAssetInfoMapper;
    //无证
    @Autowired
    private UnecoAssetInfoMapper unecoAssetInfoMapper;
    //类型
    @Autowired
    private EcoHourseTypeMapper ecoHourseTypeMapper;
    //all实体类
    @Autowired
    private AssetAreaMapper assetAreaMapper;
    @Autowired
    private AssetAllInfo assetAllInfo;
    //类型实体类
    @Autowired
    private EcoHourseType ecoHourseType;
    //有证的实体类
    @Autowired
    private UnecoAssetInfo unecoAssetInfo;
    //有证的实体类
    @Autowired
    private EcoAssetInfo ecoAssetInfo;
    //资产的实体类
    @Autowired
    private AssetBasicInfo assetBasicInfo;

/**
 *@ 描述 :公有查询
 */
    @Override
    public Result publicList(AssetQuery assetQuery) {
        Page<AssetAllInfo> objectPage = new Page<>(assetQuery.getPage(),assetQuery.getCount());
        Page<AssetAllInfo> publicAssets = assetBasicInfoMapper.publicAssets(objectPage, assetQuery.getAssetId(), assetQuery.getGroupName());
        if (publicAssets!=null) return Result.success(publicAssets);
        else return Result.error("返回的数据为null");
    }
/**
 *@ 描述 :自有查询
 */
    @Override
    public Result privateList(AssetQuery assetQuery) {
        Page<AssetAllInfo> objectPage = new Page<>(assetQuery.getPage(),assetQuery.getCount());
        Page<AssetAllInfo> privateAssets = assetBasicInfoMapper.privateAssets(objectPage, assetQuery.getAssetId(), assetQuery.getGroupName());
        System.out.println(privateAssets);
        if (privateAssets!=null) return Result.success(privateAssets);
        else return Result.error("返回的数据为null");
    }

/**
 *@ 描述 :公有+自有的添加操作 +事务操作
 */
    @Transactional
    @Override
    public Result allAdd(AssetAllInfo assetAllInfo) {
        assetBasicInfo.setAssetId(assetAllInfo.getAssetId());
        assetBasicInfo.setAddress(assetAllInfo.getAddress());
        assetBasicInfo.setAreaId(assetAllInfo.getAreaId());
        assetBasicInfo.setCompanyId(assetAllInfo.getCompanyId());
        assetBasicInfo.setHouseMark(assetAllInfo.getHouseMark());
        assetBasicInfo.setSoilMark(assetAllInfo.getSoilMark());
        assetBasicInfo.setRemark(assetAllInfo.getRemark());
        assetBasicInfo.setPublicOwner(assetAllInfo.getPublicOwner());
        assetBasicInfo.setUnuseAreaExistStatus(assetAllInfo.getUnuseAreaExistStatus());
        assetBasicInfo.setUnuseArea(assetAllInfo.getUnuseArea());
        assetBasicInfo.setUnuseReason(assetAllInfo.getUnuseReason());
        assetBasicInfo.setOwnerAreaExistStatus(assetAllInfo.getOwnerAreaExistStatus());
        assetBasicInfo.setOwnerArea(assetAllInfo.getOwnerArea());
        assetBasicInfo.setOwnReason(assetAllInfo.getOwnReason());
        assetBasicInfo.setSecurity(assetAllInfo.getSecurity());
        assetBasicInfo.setOutPicture(assetAllInfo.getOutPicture());
        assetBasicInfo.setPlanePicture(assetAllInfo.getPlanePicture());
        assetBasicInfo.setCompletionDate(assetAllInfo.getCompletionDate());
        int i1 = assetBasicInfoMapper.insert(assetBasicInfo);
        ecoAssetInfo.setAssetId(assetAllInfo.getAssetId());
        ecoAssetInfo.setEcoOwnership(assetAllInfo.getEcoOwnership());
        ecoAssetInfo.setEcoBuildArea(assetAllInfo.getEcoBuildArea());
        ecoAssetInfo.setEcoSoilArea(assetAllInfo.getEcoSoilArea());
        ecoAssetInfo.setAssetUse(assetAllInfo.getAssetUse());
        ecoAssetInfo.setEcoHourseId(assetAllInfo.getEcoHourseId());
        ecoAssetInfo.setEcoPicture(assetAllInfo.getEcoPicture());
        int i2 = ecoAssetInfoMapper.insert(ecoAssetInfo);
        unecoAssetInfo.setAssetId(assetAllInfo.getAssetId());
        unecoAssetInfo.setUnecoBuildArea(assetAllInfo.getUnecoBuildArea());
        unecoAssetInfo.setUnecoSoilArea(assetAllInfo.getUnecoSoilArea());
        int i3 = unecoAssetInfoMapper.insert(unecoAssetInfo);
        if (i1>0 && i2>0 && i3>0) return Result.success("添加成功");
        else return Result.error("添加失败，回滚！");
    }

    @Override
    public Result ListById(int id) {
        List<AssetAllInfo> assetAllInfos = assetBasicInfoMapper.assetById(id);
        if (assetAllInfos!=null)  return Result.success(assetAllInfos);
        else  return  Result.error("查询失败");
    }


    /**
     *@ 描述 :公有+自有 的修改操作+事务
     */

    @Transactional
    @Override
    public Result allUpdate(AssetAllInfo assetAllInfo) {
        assetBasicInfo.setId(assetAllInfo.getId());
        assetBasicInfo.setAssetId(assetAllInfo.getAssetId());
        assetBasicInfo.setAddress(assetAllInfo.getAddress());
        assetBasicInfo.setAreaId(assetAllInfo.getAreaId());
        assetBasicInfo.setCompanyId(assetAllInfo.getCompanyId());
        assetBasicInfo.setHouseMark(assetAllInfo.getHouseMark());
        assetBasicInfo.setSoilMark(assetAllInfo.getSoilMark());
        assetBasicInfo.setRemark(assetAllInfo.getRemark());
        assetBasicInfo.setPublicOwner(assetAllInfo.getPublicOwner());
        assetBasicInfo.setUnuseAreaExistStatus(assetAllInfo.getUnuseAreaExistStatus());
        assetBasicInfo.setUnuseArea(assetAllInfo.getUnuseArea());
        assetBasicInfo.setUnuseReason(assetAllInfo.getUnuseReason());
        assetBasicInfo.setOwnerAreaExistStatus(assetAllInfo.getOwnerAreaExistStatus());
        assetBasicInfo.setOwnerArea(assetAllInfo.getOwnerArea());
        assetBasicInfo.setOwnReason(assetAllInfo.getOwnReason());
        assetBasicInfo.setSecurity(assetAllInfo.getSecurity());
        assetBasicInfo.setOutPicture(assetAllInfo.getOutPicture());
        assetBasicInfo.setPlanePicture(assetAllInfo.getPlanePicture());
        assetBasicInfo.setCompletionDate(assetAllInfo.getCompletionDate());
        System.out.println(assetBasicInfo);
        int i1 = assetBasicInfoMapper.updateById(assetBasicInfo);
        ecoAssetInfo.setEcoId(assetAllInfo.getEcoId());
        ecoAssetInfo.setAssetId(assetAllInfo.getAssetId());
        ecoAssetInfo.setEcoOwnership(assetAllInfo.getEcoOwnership());
        ecoAssetInfo.setEcoBuildArea(assetAllInfo.getEcoBuildArea());
        ecoAssetInfo.setEcoSoilArea(assetAllInfo.getEcoSoilArea());
        ecoAssetInfo.setAssetUse(assetAllInfo.getAssetUse());
        ecoAssetInfo.setEcoHourseId(assetAllInfo.getEcoHourseId());
        ecoAssetInfo.setEcoPicture(assetAllInfo.getEcoPicture());
        int i2 = assetBasicInfoMapper.ecoInfo(ecoAssetInfo.getAssetId(),ecoAssetInfo.getEcoOwnership(),ecoAssetInfo.getEcoBuildArea(),ecoAssetInfo.getEcoSoilArea(),ecoAssetInfo.getAssetUse(),ecoAssetInfo.getEcoHourseId(),ecoAssetInfo.getEcoPicture(),ecoAssetInfo.getEcoId());
        unecoAssetInfo.setUnecoId(assetAllInfo.getUnecoId());
        unecoAssetInfo.setAssetId(assetAllInfo.getAssetId());
        unecoAssetInfo.setUnecoBuildArea(assetAllInfo.getUnecoBuildArea());
        unecoAssetInfo.setUnecoSoilArea(assetAllInfo.getUnecoSoilArea());
        int i3 =assetBasicInfoMapper.unecoInfo(unecoAssetInfo.getUnecoId(),unecoAssetInfo.getAssetId(),unecoAssetInfo.getUnecoBuildArea(),unecoAssetInfo.getUnecoSoilArea());
        if (i1>0 && i2>0 && i3>0) return Result.success("修改成功");
        else return Result.error("修改失败，回滚！");
    }
/**
 *@ 描述 :公有+自有的删除操作
 */
    @Override
    public Result allDelete(String  ids) {
        String[] split = ids.split(",");   //分割数组
        List<String> strings = Arrays.asList(split);  //转化为list数组  ？？？？？？？？
        int i = assetBasicInfoMapper.deleteBatchIds(strings);
        if (i>0) return Result.success("删除成功");
        else return Result.error("删除失败");
    }
    /**
     *@ 描述 :房产房子的类型
     */

    @Override
    public Result typeList(EcoHourseType ecoHourseType) {
        QueryWrapper<EcoHourseType> objectQueryWrapper = new QueryWrapper<>();
        List<EcoHourseType> ecoHourseTypes = ecoHourseTypeMapper.selectList(objectQueryWrapper);
        if (ecoHourseTypes!=null) return  Result.success(ecoHourseTypes);
        else return Result.success("查询失败");
    }
    /**
     *@ 描述 :区域编号类型
     */
    @Override
    public Result areaList(AssetArea assetArea) {
        QueryWrapper<AssetArea> objectQueryWrapper = new QueryWrapper<>();
        List<AssetArea> assetAreas = assetAreaMapper.selectList(objectQueryWrapper);
        if (assetAreas!=null) return Result.success(assetAreas);
        else return Result.error("查询有误");
    }

}
